<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 幕小铺科技（大连）有限公司 [ http://www.XXX.com ]
// +----------------------------------------------------------------------
// | 2020/03/14 jason wang
// +----------------------------------------------------------------------
namespace app\erp\logic;
use think\facade\Hook;
use think\DB;
use util\Tree;
use think\facade\Debug;
use think\facade\Cache;
/**
 * 逻辑层
 * @package app\erp\model
 */
class Publicslogic{

    /**
     * 购买VIP
     * @param int $timeType  购买期限 0年 1月 2永久
     * @param int $payType   支付类型 0微信 1支付宝 2财富
     * @param int $num       购买数量（年和月有效）
     * @return array
     */
    public function paySuccessOrder($orderid, $out_trade_no){
        //查询订单付款情况
        $wechat_order = factory('PayFactory', 'pay') -> getInstance($type = 'Wechat') -> getOrderByOuttrano($out_trade_no);
        
        //dump($wechat_order);
        if($wechat_order['return_code'] != 'SUCCESS') return array_out(-1001, '订单查询失败');

        if($wechat_order['trade_state'] != 'SUCCESS') return array_out(-1002, '订单没有支付成功');

        //查询订单信息
        $map = [];
        $map['ordercode'] = $orderid;
        $ErpGoodsOrderObj = model('ErpGoodsOrder') -> where($map) -> find();

        if(!$ErpGoodsOrderObj) return array_out(-1004, '订单查询失败');

        if ($ErpGoodsOrderObj -> pay_status > 0) return array_out(-1003, '该订单已完成');
        
        DB::startTrans();//启动事务
        try{
            /********行为参数赋值（*）********/
            $params = [
                'orderid' 	            => $orderid,
                'out_trade_no' 	            => $out_trade_no,
            ];
            /********行为参数赋值（*）********/
     
            /********前置行为********/
            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'before');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1002, $hook_rel['message']);//前置错误 -1002
            }
            /********前置行为********/

            /********业务逻辑********/
            //更新订单信息
            $map = [];
            $map['ordercode'] = $orderid;

            $data = [];
            $data['pay_orderid'] = 'wxpay:'.$out_trade_no;
            $data['payover_time'] = time();
            $data['pay_status'] = 1;
            
            $rel = model('ErpGoodsOrder') -> save($data, $map);

            //添加一个行为
            $params1 = [];
            $params1['order_id'] = $ErpGoodsOrderObj->id;
            $hook_rel1 = Hook::listen('order_over_after',$params1);
            
            /********业务逻辑********/

            /********后置行为********/

            $hook_rel = logic_hook($action = __FUNCTION__, $params, 'after');
            if($hook_rel['code'] < 0){
                DB::rollback();
                return array_out(-1003, $hook_rel['message']);//后置错误 -1003
            }
            /********后置行为********/

            if($rel){
                DB::commit();//file_put_contents("testvip2.txt", $orderid);
                return array_out(200, '购买成功');
            }else{
                DB::rollback();
                return array_out(-1001, '购买失败');
            }

        } catch (\Exception $e) {
            //file_put_contents("testvip1.txt", json_encode($e->getMessage()).$orderid );
            //dump($e->getMessage());
            // 回滚事务
            DB::rollback();
            return array_out(-99999, '操作失败');
        }    

    }
}